﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace PDS2
{
    /// <summary>
    /// Klass av Felix Östh
    /// 5/11-2013
    /// </summary>
    class SaveGame
    {
        private string path;


        public SaveGame(string pathToFile)
        {
            path = pathToFile;

            if (!File.Exists(path))
            {
                using (FileStream fs = new FileStream(path, FileMode.CreateNew))
                {
                }
                using (StreamWriter sw = new StreamWriter(path))
                {
                    sw.WriteLine("//New SaveGame");
                }
            }
        }


        public string GetElementValue(string element)
        {
            string[] lines = File.ReadAllLines(path);
            foreach (string s in lines)
            {
                if (s.Contains(element))
                {
                    return s.Split('=')[1];
                }
            }
            throw new Exception("Couldn't find element");
        }

        int index = -1;
        public void EditElementValue(string element, string value)
        {
            string[] lines = File.ReadAllLines(path);
            for (int i = 0; i < lines.Length; i++)
            {
                if (lines[i].Contains(element) && !lines[i].StartsWith("//"))
                {
                    index = i;
                    element = lines[i].Split('=')[0];
                    break;
                }
            }
            if(index == -1) throw new Exception("Couldn't find element");
            
            using (StreamWriter sw = new StreamWriter(path))
            {
                for (int i = 0; i < lines.Length; i++)
                {
                    if (i == index)
                    {
                        sw.WriteLine(element + "=" + value);
                    }
                    else
                    {
                        sw.WriteLine(lines[i]);
                    }
                }
            }
        }


        public void WriteNewElement(string line)
        {
            string[] lines = File.ReadAllLines(path);

            using (StreamWriter sw = new StreamWriter(path))
            {
                foreach (string s in lines)
                {
                    sw.WriteLine(s);
                }
                sw.WriteLine(line);
            }
        }

        public bool ElementExist(string element)
        {
            string[] lines = File.ReadAllLines(path);
            foreach (string s in lines)
            {
                if(s.Contains(element) && !s.StartsWith("//"))
                {
                    return true;
                }
            }

            return false;
        }

        public bool GetBool(string element)
        {
            string[] lines = File.ReadAllLines(path);
            foreach (string s in lines)
            {
                if (s.Contains(element) && !s.StartsWith("//"))
                {
                    if (s.Split('=')[1].ToLower() == "true")
                    {
                        return true;
                    }
                }
            }
            return false;
        }
    }
}
